package DRC;

import java.text.SimpleDateFormat;
import java.util.*;

import static DRC.dataItem4collect.writeData;

public class saveChannel4collect extends Thread {

    public static ArrayList<channel4collect> channels4collect = null;
    public DRCDB drcdb;

    public saveChannel4collect(DRCDB drcdb) {
        this.drcdb = drcdb;
    }


    public void saveDataItem() {
        String sqlStr = "";
        while (true) {
            for (channel4collect ch4c : channels4collect) {
                if (ch4c.enable == 1) {
                    List<String> firstList = new ArrayList<>();
                    List<List<String>> data = new ArrayList<>();
                    List<String> data2 = new ArrayList<>();
                    for (dataItem4collect di4c : ch4c.ds4c) {
                        sqlStr = "select count(1) as ct from dataItem4collect where dataItem_id='" + di4c.dataItem_id + "'";
                        List<Map<String, String>> count = drcdb.executeQuerySQL(sqlStr).datas;
                        if (Objects.equals(count.get(0).get("ct"), "0")) {
                            sqlStr = "insert into dataItem4collect(channel_id,slave_id,register_type,data_index,address,label,value,coefficient,timestamp,create_time)";
                            sqlStr += " values ";
                            sqlStr += String.format("(%d,%d,%d,%d,%d,'%s',%f,%f,'%s','%s')",
                                    di4c.channel_id, di4c.slave_id, di4c.register_type,
                                    di4c.data_index, di4c.address, di4c.label, di4c.value, di4c.coefficient,
                                    new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(di4c.timestamp),
                                    new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(di4c.create_time));
                            drcdb.executeUpdateSQL(sqlStr);
                        } else {
                            sqlStr = "update dataItem4collect set ";
                            sqlStr += String.format(" channel_id=%d,", di4c.channel_id);
                            sqlStr += String.format("slave_id=%d,", di4c.slave_id);
                            sqlStr += String.format("register_type=%d,", di4c.register_type);
                            sqlStr += String.format("data_index=%d,", di4c.data_index);
                            sqlStr += String.format("address=%d,", di4c.address);
                            sqlStr += String.format("label='%s',", di4c.label);
                            sqlStr += String.format("value=%f,", di4c.value);
                            sqlStr += String.format("coefficient=%f,", di4c.coefficient);
                            sqlStr += String.format("timestamp='%s',", new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(di4c.timestamp));
                            sqlStr += String.format("create_time='%s'", new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(di4c.create_time));
                            sqlStr += String.format("where dataItem_id=" + di4c.dataItem_id);
                            boolean b = drcdb.executeUpdateSQL(sqlStr);
                        }
                        firstList.add(String.valueOf(di4c.dataItem_id));
                        data2.add(String.valueOf(di4c.value));
                    }

                    //采集通道数据写入文件
                    data2.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(new Date()));
                    data.add(data2);
                    writeData(firstList, data, String.valueOf(ch4c.channel_id));
                }
            }
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    @Override
    public void run() {
        this.saveDataItem();
    }
}
